package MDBWP;

use strict;
use warnings;

use WordPress::XMLRPC;
use MDBUtils;

sub WPPost {

=head1 WPPost( $title )

Creates a dummy post on Wordpress with the title $title

I<Arguments>

=over

=item $title

Title of the blogpost

=back

I<Returns>

Post number

I<Exceptions>

problems with the distribution extraction, write errors on the file system, ...

=cut

    print "MDBWP::WPPost()\n";

    my $title = shift or die;
    my $config = MDBUtils::get_config();
    my $content;

    my $o = WordPress::XMLRPC->new(
        {
            username => $config->{'wordpress'}->{'username'},
            password => $config->{'wordpress'}->{'password'},
            proxy    => "http://"
              . $config->{'wordpress'}->{'host'} . "/"
              . $config->{'wordpress'}->{'path'}
              . "/xmlrpc.php"
        }
    );

    $o->server
      or die( sprintf 'MDBWP::WPPost(): could not connect with %s:%s to %s',
        $o->username, $o->password, $o->proxy );

    $content->{'categories'}             = ( ['Uncategorized'] );
    $content->{'mt_allow_comments'}      = 1;
    $content->{'title'}                  = $title;
    $content->{'userid'}                 = '1';
    $content->{'wp_author_display_name'} = 'Updatebot';
    $content->{'description'}            = '<a href="http://malwaredatabase.net/w/index.php?title=' . $title . '">Malware analysis</a>';

    my $post_id = $o->newPost($content) or die( $o->errstr );
    return ($post_id);
}

1;
